/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wanhai.itm.util;

import com.wanhai.util.RecordProcessBean;
import com.wanhai.zkbase.dao.IBaseDAO;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.zul.AbstractListModel;
import org.zkoss.zul.ext.Sortable;

/**
 *
 * @author m2434
 */
public class LiveDataModel<T extends RecordProcessBean> extends AbstractListModel implements Sortable{
    private Log logger = LogFactory.getLog(getClass());
    private IBaseDAO dao;
    private T cond;
    private int pageSize = 20;//default pageSize
    private int totalSize = 0;
    private String orderBy = "";
    private Map<Integer, T> catchMap = new HashMap<Integer, T>();
    
    
     
    public LiveDataModel(IBaseDAO dao, T cond){
        this.dao = dao;
        this.cond = cond;
        this.setMultiple(true);//default 設為多選
    }
     
    public T getElementAt(int index) {
        T targetVO = catchMap.get(index);
        
        if (targetVO == null){
            try{
                int page = (index / pageSize)+1;
                List<T> tempResult = dao.partialQuery(cond,page,pageSize,null,orderBy);
                
                System.out.println("new query");
                
                int indexKey = index;
                for (T o : tempResult ){
                    catchMap.put(indexKey, o);
                    indexKey++;
                }
            }catch(Exception e){
                logger.error(e.getMessage(),e);
            }
        }else{
            return targetVO;
        }

        targetVO = catchMap.get(index);
        if (targetVO == null){
            throw new RuntimeException("Element at index "+index+" cannot be found in the database.");
        }else{
            return targetVO;
        }
    }
     
    
    public int getSize() {
        try{
            if(totalSize == 0){
                totalSize = dao.getQueryCount(cond);
            }
        }catch(Exception e){
            logger.error(e.getMessage(),e);
        }
        
        return totalSize;
    }
    
    
    
    public void setOrderBy(String orderBy){
        this.orderBy = orderBy;
    }

    public List<T> getPageResult(int page) {
        List returnList = new ArrayList<T>();
        int start = page * getPageSize();
        int end = (page+1) * getPageSize() - 1;

        for(int i=start; i<=end ; i++){
            returnList.add(getCatchMap().get(i));
        }
        
        return returnList;
    }

    public Map<Integer, T> getCatchMap() {
        return catchMap;
    }
    
    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    
    @Override
    public void sort(Comparator cmpr, boolean ascending) {
    }
    
    @Override
    public String getSortDirection(Comparator cmpr) {
        return null;   
    }
}
